///////////////////////////////////////////////////////////////////////////////////////////////
//
//  Copyright 2014 Remon de Boer
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//
//  GWT-AJ - Test - Webserver 1.0
//
///////////////////////////////////////////////////////////////////////////////////////////////////

package net.atremmedia.gwt.actionjava.examples.webserver;

import java.io.IOException;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.component.LifeCycle;
import org.eclipse.jetty.util.component.LifeCycle.Listener;
import org.eclipse.jetty.webapp.WebAppContext;

/**
 * <summary>net.atremmedia.gwt.actionjava.test.webserver.TestWebserver</summary>
 * <ul>
 * <li><b>Company: </b><a href="http://www.atremmedia.net">atRem Interactive Media B.V</a></li>
 * <li><b>Project: </b>GWT-AJ - Test - Webserver</li>
 * </ul>
 *
 * @author boerr
 * @since 1.0
 */
public class TestWebserver
{
	/**
	 * <summary>Instantiates a new test webserver.</summary>
	 *
	 * @since 1.0
	 * @author boerr
	 * @see TestWebserver
	 */
	public TestWebserver()
	{
		(new Thread(new Runnable()
		{

			@Override
			public void run()
			{
				try {
					System.in.read();
				} catch (IOException e) {
					e.printStackTrace();
				}
				System.exit(0);
			}
		})).start();
		initialize(2222, "target/war");
	}

	/**
	 * <summary>Initialize.</summary>
	 *
	 * @param port the port
	 * @param warFile the war file
	 * @return void
	 * @since 1.0
	 */
	public void initialize(int port, String warFile)
	{
		Server server = new Server(port);
		WebAppContext webapp = new WebAppContext();
		webapp.setContextPath("/");
		webapp.setWar(warFile);
		server.setHandler(webapp);
		server.addLifeCycleListener(new Listener()
		{
			@Override
			public void lifeCycleStopping(LifeCycle event)
			{
				// Not used, ignore
			}

			@Override
			public void lifeCycleStopped(LifeCycle event)
			{
				// Not used, ignore
			}

			@Override
			public void lifeCycleStarting(LifeCycle event)
			{
				// Not used, ignore
			}

			@Override
			public void lifeCycleStarted(LifeCycle event)
			{
			}

			@Override
			public void lifeCycleFailure(LifeCycle event, Throwable cause)
			{
				// Not used, ignore
			}
		});

		try {
			server.start();
		} catch (Exception ignore) {}

	}

	/**
	 * <summary>The main method of GWT-AJ - Test - Webserver.</summary>
	 *
	 * @param args {@link String}[]
	 * @author boerr
	 * @since 1.0
	 */
	public static void main(String[] args)
	{
		new TestWebserver();
	}
}
